package com.csk.zsk.compents.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.encode.JsonBinder;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.Book;
import com.csk.zsk.compents.entity.BookChapters;
import com.csk.zsk.compents.service.BookService;

@Controller
@Scope("prototype")
public class BookAction extends AjaxActionSupport {

	
	private static final long serialVersionUID = -5770126200775078417L;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();
	@Autowired
	private BookService bookService;
	
	private String bookId;
	private String bookLikeName;
	private String bookChaptersId;
	private String startIndex;
	public String getBookId() {
		return bookId;
	}
	public void setBookId(String bookId) {
		this.bookId = bookId;
	}
	public String getBookLikeName() {
		return bookLikeName;
	}
	public void setBookLikeName(String bookLikeName) {
		this.bookLikeName = bookLikeName;
	}
	public String getBookChaptersId() {
		return bookChaptersId;
	}
	public void setBookChaptersId(String bookChaptersId) {
		this.bookChaptersId = bookChaptersId;
	}
	public String getStartIndex() {
		return startIndex;
	}
	public void setStartIndex(String startIndex) {
		this.startIndex = startIndex;
	}
	
	
	/**
	 * 获取小说对象
	 */
	public void getBookToJsonById(){
		if(bookId==null||bookId.equals("")){
			return;
		}
		Book book = bookService.searchBookById(bookId);
		String json = binder.toJson(book);
		Struts2Utils.renderJson(json);
	}
	/**
	 * 通过名字获取小说列表
	 */
	public void getBookListToJsonByLikeName(){
		if(bookLikeName==null||bookLikeName.equals("")){
			return;
		}
		List<Book> bookList = bookService.searchBookByLikeName(bookLikeName);
		String json = binder.toJson(bookList);
		Struts2Utils.renderJson(json);
	}
	/**
	 * 获取小说章节列表
	 */
	public void getBookChaptersListToJsonByBookId(){
		if(bookId==null||bookId.equals("")){
			return;
		}
		//Book book = bookService.searchBookById(bookId);
		List<BookChapters> bookChaptersList =  bookService.searchBookChaptersByBookId(bookId);
		
		String json = binder.toJson(bookChaptersList);
		Struts2Utils.renderJson(json);
	}
	/**
	 * 获取小说章节对象
	 */
	public void getBookChaptersToJsonById(){
		if(bookChaptersId==null||bookChaptersId.equals("")){
			return;
		}
		BookChapters bookChapters = bookService.searchBookChaptersById(bookChaptersId);
		String json = binder.toJson(bookChapters);
		Struts2Utils.renderJson(json);
	}


	/**
	 * 获取排名前10的小说
	 */
	public void searchTop10ByOrder(){
		if(startIndex==null||startIndex.equals("")){
			startIndex = "0";
		}
		List<Book> bookList = bookService.searchTop10ByOrder(startIndex);
		String json = binder.toJson(bookList);
		Struts2Utils.renderJson(json);
		
	}
	/**
	 * 获取推荐前10的小说
	 */
	public void searchTop10ByRec(){
		if(startIndex==null||startIndex.equals("")){
			startIndex = "0";
		}
		List<Book> bookList = bookService.searchTop10ByRec(startIndex);
		String json = binder.toJson(bookList);
		Struts2Utils.renderJson(json);
	}
	/**
	 * 获取随机前10的小说
	 */
	public void searchTop10ByRandom(){
		List<Book> bookList = bookService.searchTop10ByRandom();
		String json = binder.toJson(bookList);
		Struts2Utils.renderJson(json);
	}
	
	
	
	
	
	
	
	
	
	
	
	

}
